﻿@page "/songslist/"
@page "/songslist/{PageNumber:int}"
@page "/songslist/{SearchTerms}/{PageNumber:int}"
@using MusicX.Web.Shared.Songs

<ErrorsList Errors="response?.Errors" />

@if (this.response?.Data?.Songs == null)
{
    <p><em>Loading...</em></p>
}
else if (!this.response.Data.Songs.Any())
{
    <p><em>No songs found.</em></p>
}
else
{
    foreach (var song in this.response.Data.Songs)
    {
        <SongCard Song="@song"></SongCard>
    }

    <div class="clearfix"></div>

    <Pagination Page="@PageNumber"
                TotalPages="@this.response?.Data.Pages"
                Radius="2"
                SelectedPage="ChangePage" />
}

@code {
    [Parameter]
    public int PageNumber { get; set; } = 1;

    [Parameter]
    public string SearchTerms { get; set; } = string.Empty;

    ApiResponse<SongsListResponseModel> response;

    protected override async Task OnInitializedAsync()
    {
        this.response = await this.ApiClient.GetSongsList(this.PageNumber, this.SearchTerms);
    }

    async Task ChangePage(int page)
    {
        this.PageNumber = page;
        this.response = await this.ApiClient.GetSongsList(page, this.SearchTerms);

        if (!string.IsNullOrWhiteSpace(this.SearchTerms))
        {
            this.NavigationManager.NavigateTo($"songslist/{this.SearchTerms}/{page}");
        }
        else
        {
            this.NavigationManager.NavigateTo($"songslist/{page}");
        }
    }
}
